package com.example.llollama.controller;

/**
 * @Description: TODO
 * @author: lonely
 * @date: 2024年10月19日 0:00
 */
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/function")
@RequiredArgsConstructor
@RestController
public class FunctionController {

    private final ChatModel chatModel;

    @GetMapping("/weather")
    public String testFunction(@RequestParam String msg) {
        UserMessage userMessage = new UserMessage(msg);
        OllamaOptions options = OllamaOptions.builder()
                .withFunction("locationWeatherFunction")
                .withModel("llama3.1")
                .build();
        ChatResponse call = chatModel.call(new Prompt(List.of(userMessage),options)
        );
        return call.getResult().getOutput().getContent();
    }
    @GetMapping("/weather2")
    public String weather2(@RequestParam String msg) {

        UserMessage userMessage = new UserMessage("请用中文回答："+msg);
        OllamaOptions options = OllamaOptions.builder()
                .withFunction("locationWeatherFunction")
                .withModel("llama3.1")
                .build();
        ChatResponse call = chatModel.call(new Prompt(List.of(userMessage),options)
        );
        return call.getResult().getOutput().getContent();
    }

}

